package cn.itsource.quartz.domain;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Data
public class QuartzInfo {
    private String jobName;//任务的名字
    private Class cla; //具体的任务的类的Class对象
    private Date time;
    private Map<String,Object> params = new HashMap<>();
    private String corn;

    //把时间转换为corn 表达 “0/1 * * * * ?”

    public void setTime(Date time){
        //把time 转换为 corn表达式
        this.time = time;
        String[] cronArr = new String[7];
        for (int i = 0; i < cronArr.length; i++) {
            cronArr[i] = "";
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        int second = calendar.get(Calendar.SECOND);
        int minute = calendar.get(Calendar.MINUTE);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int month = calendar.get(Calendar.MONTH) + 1;
        int year = calendar.get(Calendar.YEAR);

        cronArr[0] = second + "";
        cronArr[1] = minute + "";
        cronArr[2] = hour + "";

        cronArr[3] = day + "";
        cronArr[4] = month + "";
        cronArr[5] = "?";
        cronArr[6] = year + "";

        String cron = StringUtils.join(cronArr," ").trim();

        this.corn=cron;

    }


}
